package com.cskaoyan.controller.wx_coupon_controller;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.User;
import com.cskaoyan.bean.wx_couponBO.ExchangeBO;
import com.cskaoyan.bean.wx_couponBO.ReceiveBO;
import com.cskaoyan.bean.wx_couponVO.DataBean;
import com.cskaoyan.bean.wx_couponVO.WxCouponListVO;
import com.cskaoyan.bean.wx_couponVO.WxSelectListVO;
import com.cskaoyan.bean.wx_couponVO.WxUserCouponsVO;
import com.cskaoyan.service.promotionService.CouponService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author 陈泉江
 * @version 1.0
 * @description: TODO
 * @date 2021/6/7 20:30
 */

@RestController
@RequestMapping("/wx/coupon/")
public class WxCouponController {

    @Autowired
    CouponService couponService;

    @RequestMapping("list")
    public BaseRespVo wxCouponList(Integer page, Integer size) {
        //复用了后台的优惠券类
        WxCouponListVO couponListVO = couponService.wxCouponList(page, size);

        return BaseRespVo.ok(couponListVO);
    }


    @RequestMapping("mylist")
    public BaseRespVo myCouponList(Integer page, Integer size, Short status) {
        //复用了后台的用户优惠券类

        User user = new User();
        Subject subject = SecurityUtils.getSubject();
        Object subjectPrincipal = subject.getPrincipal();
        if (subjectPrincipal == null) {
            return BaseRespVo.fail("请登录后查看");
        } else {
            user = (User) subjectPrincipal;
        }
        WxUserCouponsVO userCouponsVO = couponService.myCouponList(page, size, status,user.getId());

        return BaseRespVo.ok(userCouponsVO);
    }


    /**
     * 当前订单可用优惠券列表
     *
     * @author 陈泉江
     * @date 2021/6/7
     */

    @RequestMapping("selectlist")
    public BaseRespVo selectlist(Integer cartId, Integer grouponRulesId) {
        //购物车id没啥用，直接用用户id
        User user = new User();
        Subject subject = SecurityUtils.getSubject();
        Object subjectPrincipal = subject.getPrincipal();
        if (subjectPrincipal == null) {
            return BaseRespVo.fail("请登录后查看");
        } else {
            user = (User) subjectPrincipal;
        }
        List<DataBean> dataBeans = couponService.selectlistCoupon(user.getId());
        return BaseRespVo.ok(dataBeans);
    }


    //优惠券领取
    @RequestMapping("receive")
    public BaseRespVo receive(@RequestBody ReceiveBO receiveBO) {

        User user = new User();
        Subject subject = SecurityUtils.getSubject();
        Object subjectPrincipal = subject.getPrincipal();
        if (subjectPrincipal == null) {
            return BaseRespVo.fail("请登录后查看");
        } else {
            user = (User) subjectPrincipal;
        }

        //根据返回的状态码，判断用户是否领取过该优惠券
        int code = couponService.receiveCoupon(receiveBO.getCouponId(),user.getId());
        if (code == 0) {
            return BaseRespVo.fail(704, "优惠券已经领取过");
        } else if (code == 1) {
            return BaseRespVo.ok();
        } else if (code == 2) {
            return BaseRespVo.fail();
        } else {
            return BaseRespVo.fail(500,"领取数量达到上限");
        }
    }

    //优惠券兑换
    @RequestMapping("exchange")
    public BaseRespVo exchange(@RequestBody ExchangeBO exchangeBO) {

        User user = new User();
        Subject subject = SecurityUtils.getSubject();
        Object subjectPrincipal = subject.getPrincipal();
        if (subjectPrincipal == null) {
            return BaseRespVo.fail("请登录后查看");
        } else {
            user = (User) subjectPrincipal;
        }


        //根据返回的状态码，判断用户的优惠码是否过期
        int code = couponService.exchangeCoupon(exchangeBO.getCode(),user.getId());
        if (code == 0) {
            return BaseRespVo.fail(704, "优惠券已经领取过");
        } else if (code == 1) {
            return BaseRespVo.ok();
        } else if (code == 2) {
            return BaseRespVo.fail();
        } else if (code == 3) {
            return BaseRespVo.ok(500, "领取数量达到上限");
        } else if (code == 4) {
            return BaseRespVo.ok(500, "优惠券已过期");
        }
        return BaseRespVo.fail(500, "优惠券已下架");
    }

}
